<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The Poster class for table 'poster'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Definitions.
// Column of status reserves.

// Just the scale.
define('POSTER_SINGLE_SIZE',			95 * 2);
define('POSTER_DOUBLE_SIZE',			(95 * 2) * 2);
define('POSTER_MAX_HIGHT',				115 * 2);

require_once INSTALLDIR . '/classes/Memcached_DataObject.php';

class Poster extends Memcached_DataObject
{
	public $__table = 'poster';				// table name
	public $id;								// int(4)  primary_key not_null
	public $news_id;						// int(4)  not_null unique foreign_key
	public $status;							// tinyint(4)
	public $size;							// int(11)
	public $postername;						// varchar(255)  not_null
	public $modified;						// datetime()  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('Poster', $key, $value); 
	}
	
	/**
	 * Delete the poster both in database and filesystem.
	 */
	function delete()
	{
		@unlink($this->posterpath());
		@unlink($this->thumbnailpath());
		
		parent::delete();
	}
	
	/**
	 * Remove the poster item if in the air.
	 * 
	 * While a new poster is uploaded successfully, then check the old one!
	 * See the uploadedposter_id is existed already, then clean it up!
	 * Or the micro news fail to post. Then clean it up.
	 */
	static function removeIfAiredPoster($poster_id)
	{
		if ( !empty($poster_id)) {
			if ( !Poster::checkIfSetNewsid($poster_id)) {
				// If not set, then consider it's a poster item in air, most probable being
				// replace by another newly uploading pirture! Delete it!
				$uploadedposter = Poster::staticGet('id', $poster_id);
				$uploadedposter->delete();
			}
		}
	}
	
	/**
	 * Check the poster by given poster_id if it's new_id is set.
	 * If so, or no such poster, it's abusive.
	 * 
	 * 0 is the default null poster singnal.
	 * 
	 * @return true, set, false, not set.
	 */
	static function checkIfSetNewsid($poster_id)
	{
		if (0 == intval($poster_id)) {
			return false;	
		}
		
		$query = 'SELECT `news_id` FROM `poster` ' .
				 'WHERE `id` = ' . $poster_id;
		
		$poster = new Poster();
		$poster->query($query);
		
		$result = true;
		if ($poster->fetch()) {
			if ( empty($poster->new_id)) {
				$result = false;
			}
		}
		
		$poster->free();
		$poster = null;
		
		return $result;
	}
	
	/**
	 * Set the news_id for the poster item which is belong to one successfully
	 * posted micronews.
	 */
	static function setNewsidById($news_id, $poster_id)
	{
		$query = 'UPDATE `poster` SET `news_id` = ' . $news_id .
				 ' WHERE `id` = ' . $poster_id;
		
		$poster = new Poster();
		$poster->query($query);
		
		$result = false;
		if ($poster->fetch()) {
			$result = true;
		}
		
		$poster->free();
		$poster = null;
		
		return $result;
	}
	
	/**
	 * Add the infomation of poster into database.
	 */
	static function add($fields)
	{
		// Magically put fields into current scope.
		extract($fields);
		
		$poster = new Poster();
		$poster->status = 0;
		$poster->news_id = $news_id;
		$poster->postername = $postername;
		$poster->size = $size;
		$poster->created = slh_sql_now();
		
		$poster_id = $poster->insert();
		if (empty($poster_id)) {
			slh_log_db_error($poster, 'INSERT', __FILE__);
			return false;
		}
		
		return $poster_id;
	}
	
	/**
	 * Get the thumbnail url by given news_id.
	 */
	static function getThumbnailUrlByNewsid($news_id)
	{
		$query = 'SELECT `postername` FROM `poster` ' .
				 'WHERE `news_id` = ' . $news_id;
		
		$poster = new Poster();
		$poster->query($query);
		
		$result = false;
		if ($poster->fetch()) {
			$result = Poster::url(Poster::filename2Thumbnailname($poster->postername));
		}
		
		$poster->free();
		$poster = null;
		
		return $result;
	}
	
	/**
	 * Get the poster path.
	 */
	function posterpath()
	{
		return Poster::path($this->postername);
	}
	
	/**
	 * Get the poster url.
	 */
	function posterurl()
	{
		return Poster::url($this->postername);
	}
	
	/**
	 * Get the thumbnail path.
	 */
	function thumbnailpath()
	{
		return Poster::path(Poster::filename2Thumbnailname($this->postername));
	}
	
	/**
	 * Get the thumbnail url.
	 */
	function thumbnailurl()
	{
		return Poster::url(Poster::filename2Thumbnailname($this->postername));
	}
	
	/**
	 * Change to original filename(postername) to thumbnail name.
	 * TODO: Fix the naming standard, more simplified?
	 */
	static function filename2Thumbnailname($filename)
	{
// FIXME: All the 'o' would be changed to 't'!!
		return strtr($filename, array('o' => 't'));
	}
	
	/**
	 * Change to temp filename(storage) to poster name.
	 */
	static function tempname2Postername($tempname)
	{
// FIXME: All the 'temp' would be changed to ''!!
		return strtr($tempname, array('temp' => ''));
	}
	
	/**
	 * The barename of the original file.
	 */
	static function filename($id, 
							 $extension, 
							 $extra = null)
	{
		return 'o-' . $id . (($extra) ? ('-' . $extra) : '') . $extension;
	}
	
	/**
	 * The absolute path including the barename. (../xx.jpg).
	 */
	static function path($filename)
	{
		$dir = slh_config('poster', 'dir');
		
		if ($dir[strlen($dir) - 1] != '/') {
			$dir .= '/';
		}
		
		return $dir . $filename;
	}
	
	/**
	 * The unique url.
	 */
	static function url($filename)
	{
		$path = slh_config('poster', 'path');
		
		if ($path[strlen($path) - 1] != '/') {
			$path .= '/';
		}
		
		if ($path[0] != '/') {
			$path = '/' . $path;
		}
		
		// FIXME: The extra server for avatar.
		$server = slh_config('poster', 'server');
		
		return 'http://' . $server . $path . $filename;
	}
	
	// Fake default image.
	static function defaultPoster()
	{
		return slh_config('poster', 'path') . 'default.jpg';
	}
}

?>